package com.originskyseed.platform.count.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.originskyseed.platform.count.model.FinancialManage;
import com.originskyseed.platform.count.service.IFinancialManageService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.count.i18n.CountResponseMsg;
import com.originskyseed.platform.count.model.FeeCategory;
import com.originskyseed.platform.count.service.IFeeCategoryService;
import com.originskyseed.platform.count.vo.request.FeeCategoryRequestVO;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 费用类别表Controller类（OSI_FEE_CATEGORY）
 * 
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/feeCategory")
@BaseLocaleResource(localeFileName = "i18n_count")
public class FeeCategoryController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(FeeCategoryController.class);

	@DubboReference
	private IFeeCategoryService feeCategoryService;

	@DubboReference
	private IFinancialManageService financialManageService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/feeCategoryList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<FeeCategory>> feeCategoryList(HttpServletRequest request) {
		BaseResponseBody<BasePage<FeeCategory>> result = null;
		try {
			// 请求对象
			FeeCategoryRequestVO requestVO = this.getParameterBean(FeeCategoryRequestVO.class);
			requestVO.setIsDel(0);
			// 分页查询
			BasePage<FeeCategory> page = new BasePage<FeeCategory>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.feeCategoryService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<FeeCategory>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.LIST_LOAD_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<FeeCategory>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.LIST_LOAD_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/feeCategorySave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<FeeCategory> feeCategorySave(HttpServletRequest request) {
		BaseResponseBody<FeeCategory> result = null;
		try {
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 请求对象
			FeeCategory feeCategory = this.getParameterBean(FeeCategory.class);

			if (StringUtils.isNotBlank(feeCategory.getId())) {
				this.feeCategoryService.updateByPrimaryKey(feeCategory);
			} else {
				feeCategory.setCreator(userInfo.getName());
				feeCategory.setCreatorId(userInfo.getId());
				feeCategory.setIsEnable(1);
				feeCategory.setIsDel(0);
				this.feeCategoryService.insertSelective(feeCategory);
			}
			result = new BaseResponseBody<FeeCategory>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SAVE_SUCCESS.getMsg()), feeCategory);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<FeeCategory>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/feeCategoryDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> feeCategoryDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			boolean res=true;
            if(ids!=null&&ids.length>0)
			{
				for(int i=0;i<ids.length;i++)
				{
					String id=ids[i];
					FinancialManage financialManage=new FinancialManage();
					financialManage.setFeeCategoryId(id);
					List<FinancialManage> list=financialManageService.selectList(financialManage);
					if(list!=null&&list.size()>0)
					{
						res=false;
						break;
					}
				}
			}
            if(!res)
			{
				result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_DELETE_NO_RIGHT.getMsg()),null, null);
				return result;
			}
			this.feeCategoryService.updateByIds(Arrays.asList(ids),1);
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 列表查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectFeeCategoryList.json", method = RequestMethod.POST)
	public BaseResponseBody selectFeeCategoryList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			FeeCategory feeCategory = this.getParameterBean(FeeCategory.class);
			feeCategory.setIsDel(0);
			feeCategory.setIsEnable(1);

			List<FeeCategory> feeCategoryList = this.feeCategoryService.selectList(feeCategory);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.LIST_LOAD_SUCCESS.getMsg()), feeCategoryList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.LIST_LOAD_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询详情
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectFeeCategoryById.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody<FeeCategory> selectFeeCategoryById(HttpServletRequest request) {
		BaseResponseBody<FeeCategory> result = null;
		try {
			// 请求id
			String id = this.getParameterString("id", null);
			FeeCategory feeCategory = this.feeCategoryService.selectByPrimaryKey(id);
			result = new BaseResponseBody<FeeCategory>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SAVE_SUCCESS.getMsg()), feeCategory);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<FeeCategory>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}